#include "doble_list.h"
#include "showbmp.h"
#include "font.h"

int myslInit(SL *head)
{
    // head = (SL *)malloc(sizeof(SL));
    if (head == NULL)
    {
        printf("head error..\n");
        return ERROR;
    }

    bzero(head->path, sizeof(head->path));
    head->count = 0;
    head->next = NULL;
    head->up = NULL;

    // return head;
}

int insert(SL *head, char *path)
{
    SL *Node = (SL *)malloc(sizeof(SL));
    if (Node == NULL)
    {
        printf("back error..\n");
        return ERROR;
    }

    strcpy(Node->path, path);      // data
    Node->count = head->count + 1; //元素个数
    min = max = head->count = Node->count;

    SL *p = NULL;
    for (p = head; p->next != NULL; p = p->next)
    {
        if (p->count == 1)
        {
            p->up = NULL;
        }
    }
    p->next = Node; //下
    Node->up = p;   //上
    Node->next = NULL;

    return OK;
}

//函数功能：双向链表——下
//参数说明：head头，stage位置，type类型1图片，2视频，3音乐
//返回值  ：stage位置
int cheak_down(SL *head, int stage, int type)
{
    SL *p = NULL;
    for (p = head->next; p != NULL; p = p->next)
    {
        if (p->count == stage)
        {
            printf("count %d path %s\n", p->count, p->path);
            if (type == 1) //图片
            {
                show_bmp(p->path, 0, 0);
            }
            if (type == 2) //视频
            {
                show_bmp("./bfq.bmp", 0, 430);
                Display_characterX(0,      // x坐标起始点
                                   430,      // y坐标起始点
                                   p->path,  // GB2312 中文字符串
                                   0xFF0000, //字体颜色值
                                   1);
                char cmd[100] = "";
                system("killall -9 mplayer");
                sprintf(cmd, "mplayer -slave -quiet -geometry 0:0 -zoom -x 800 -y 430 -input file=/tmp/fifo %s &", p->path);
                system(cmd);
            }
            if (type == 3) // mp3
            {   
                show_bmp("./bfq.bmp", 0, 430);
                Display_characterX(0,      // x坐标起始点
                                   430,      // y坐标起始点
                                   p->path,  // GB2312 中文字符串
                                   0xFF0000, //字体颜色值
                                   1);
                char cmd1[100] = "";
                system("killall -9 mplayer");
                sprintf(cmd1, "mplayer -slave -quiet -geometry 0:0 -zoom -x 800 -y 430 -input file=/tmp/fifo %s &", p->path);
                system(cmd1);
            }
            printf("\n");
            stage++;
            break;
        }
    }
    if (stage > min)
    {
        stage = 1;
    }

    return stage;
}

//函数功能：双向链表——上
//参数说明：head头，stage位置，type类型1图片，2视频，3音乐
//返回值  ：stage位置
int cheak_up(SL *head, int stage, int type)
{
    SL *p = NULL;
    SL *q = NULL;
    for (p = head->next;; p = p->next)
    {
        if (p->next == NULL)
        {
            q = p;
            break;
        }
    }

    for (q = p; q != NULL; q = q->up)
    {
        if (q->count == stage)
        {
            printf("count %d path %s\n", q->count, q->path);
            if (type == 1) //图片
            {
                show_bmp(q->path, 0, 0);
            }
            if (type == 2) //视频
            {
                show_bmp("./bfq.bmp", 0, 430);
                Display_characterX(0,      // x坐标起始点
                                   430,      // y坐标起始点
                                   q->path,  // GB2312 中文字符串
                                   0xFF0000, //字体颜色值
                                   1);
                char cmd[100] = "";
                system("killall -9 mplayer");
                sprintf(cmd, "mplayer -slave -quiet -geometry 0:0 -zoom -x 800 -y 430 -input file=/tmp/fifo %s &", q->path);
                system(cmd);
            }
            if (type == 3) // mp3
            {
                show_bmp("./bfq.bmp", 0, 430);
                Display_characterX(0,      // x坐标起始点
                                   430,      // y坐标起始点
                                   q->path,  // GB2312 中文字符串
                                   0xFF0000, //字体颜色值
                                   1);
                char cmd[100] = "";
                system("killall -9 mplayer");
                sprintf(cmd, "mplayer -slave -quiet -geometry 0:0 -zoom -x 800 -y 430 -input file=/tmp/fifo %s &", q->path);
                system(cmd);
            }
            printf("\n");
            stage--;
            break;
        }
    }
    if (stage == 0)
    {
        stage = max;
    }
    return stage;
}

// int main()
// {
//     int stage = 1, d = 0;
//     SL *head = NULL;
//     head = myslInit(head);

//     for (int i = 1; i <= 5; i++)
//     {
//         insert(head, "ll"); //增
//     }
//     while (1)
//     {
//         printf("up 1  down 2");
//         scanf("%d", &d);
//         switch (d)
//         {
//         case 1:
//             stage = cheak_up(head, stage);
//             break;
//         case 2:
//             stage = cheak_down(head, stage);
//             break;

//         default:
//             break;
//         }
//     }

//     system("pause");
// }